//
//  PopoverPresentationController.swift
//  Super_WeiBo
//
//  Created by 梁超 on 16/3/16.
//  Copyright © 2016年 lc. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {

    /// 接受弹出视图frame
    var presentFrame = CGRectZero
    // 创建并初始化转场动画对象
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    
    // 在即将布局时 重新调整视图大小
    override func containerViewWillLayoutSubviews() {
        
        // 修改内容视图的大小
        if presentFrame == CGRectZero{
            presentedView()?.frame = CGRect(x: 100, y: 56, width: 200, height: 200)
        }else{
            presentedView()?.frame = presentFrame
        }
        
        containerView?.insertSubview(coverView, atIndex: 0)
        
    }
    
    private lazy var coverView : UIView = {
       
        let vi = UIView()
        vi.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        vi.frame = UIScreen.mainScreen().bounds
        let tap = UITapGestureRecognizer(target: self, action: "close")
        vi.addGestureRecognizer(tap)
        
        return vi
    }()
    
    func close()
    {
        // 拿到当前弹出的控制器
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
}
